// pages/tabBar/my/component/Information/resetPass/resetPass.js
import { request } from '../../../../../../utils/util'
const { $Message } = require('../../../../../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pass1: "",
    pass2: "",
    isShowTool: false,
    id: ""
  },
  onLoad() {
    const id = wx.getStorageSync('userInfo').id
    this.setData({
      ...this.data,
      id: id
    })
  },
  /**
   * 重置密码按钮
   */
  sure() {
    const that = this
    if (this.data.pass1 === "" || this.data.pass2 === "") {
      wx.showToast({
        title: '请输入要修改的密码',
        icon: 'none'
      })
      return
    }
    if (this.data.pass1 !== this.data.pass2) {
      this.setData({
        ...this.data,
        pass2: "",
        isShowTool: true
      })
      return
    }
    const param = {
      password: this.data.pass2
    }
    request("PATCH", `/api/auth/user/${this.data.id}/`, param, "", true, "正在修改")
      .then(res => {
        if (res.statusCode === 200) {
          wx.showModal({
            showCancel:"false",
            title: "修改成功",
            content: "您的密码已修改，登录即将过期，请重新登陆",
            success(res) {
              if (res.confirm) {
                wx.clearStorage();
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '修改失败，请联系系统管理员',
            icon: "none"
          })
        }
      })

  },

  focus() {
    this.setData({
      ...this.data,
      isShowTool: false
    })
  }

})